/*
 * Copyright 2003-2007 (c) Live Media Pty Ltd. <argot@einet.com.au> 
 *
 * This software is licensed under the Argot Public License 
 * which may be found in the file LICENSE distributed 
 * with this software.
 *
 * More information about this license can be found at
 * http://www.einet.com.au/License
 * 
 * The Developer of this software is Live Media Pty Ltd,
 * PO Box 4591, Melbourne 3001, Australia.  The license is subject 
 * to the law of Victoria, Australia, and subject to exclusive 
 * jurisdiction of the Victorian courts.
 */

package com.colony.networkvm.instruction;

import java.io.IOException;

import com.argot.TypeException;
import com.argot.TypeLibraryWriter;
import com.argot.TypeMap;
import com.argot.TypeOutputStream;
import com.argot.TypeWriter;
import com.argot.common.BigEndianUnsignedShort;
import com.colony.networkvm.machine.NvmInstance;
import com.colony.remote.RemoteException;

public class NvmLoadHeapObject
implements NvmInstruction
{
	public static final String TYPENAME = "nvm.instruction.load_heap_object";
	
	public int _index;
	
	public NvmLoadHeapObject(int index )
	{
		_index = index;
	}
	
    public void execute(NvmInstance request) 
    throws RemoteException
    {
        request.incProgramCounter();
        request.push( request.getHeapObject(_index));
    }

    private static class NvmLoadHeapObjectTypeWriter
    implements TypeWriter
    {
    	private TypeWriter _uint16;
    	
    	public NvmLoadHeapObjectTypeWriter(TypeMap map)
    	throws TypeException
    	{
    		_uint16 = map.getWriter(map.getId(BigEndianUnsignedShort.TYPENAME));
    	}
    	
	    public void write(TypeOutputStream out, Object o) throws TypeException, IOException
	    {
	    	NvmLoadHeapObject instruction = (NvmLoadHeapObject) o;
	    	int id = instruction._index;
	    	
	        _uint16.write( out, new Integer(id));
	    }
    }
    
    
    public static class NvmLoadHeapObjectTypeLibraryWriter
    implements TypeLibraryWriter
    {

		public TypeWriter getWriter(TypeMap map) 
		throws TypeException 
		{
			return new NvmLoadHeapObjectTypeWriter(map);
		}
    }

}
